package com.jinyu.jyly.ui.mine.address.change_address;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.adapter.ChangeOneMenuAdapter;
import com.jinyu.jyly.adapter.ChangeThreeAdapter;
import com.jinyu.jyly.adapter.ChangeTwoAdapter;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.base.IPresenter;
import com.jinyu.jyly.bean.ChoiceAddressListInfo;
import com.jinyu.jyly.utils.DisplayCutoutUtil;
import com.jinyu.jyly.utils.GetJsonDataUtil;


import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 选择地址
 *
 * @author 62485
 */
public class ChangeAddressActivity extends BaseActivity {

    @BindView(R.id.rcv_two)
    RecyclerView rcv_two;
    @BindView(R.id.rcv_three)
    RecyclerView rcv_three;
    @BindView(R.id.rcv_one)
    RecyclerView rcv_one;
    @BindView(R.id.fake_status_bar)
    View fake_status_bar;
    @BindView(R.id.iv_back)
    ImageView iv_back;

    //    private List<ProvinceAndCityInfo.DataBean> mBeanList;
    private String mProvinceValue;
    private String mProvinceLabel;
    private String mCityValue;
    private String mCityLabel;
    private ChangeThreeAdapter mChangeThreeAdapter;
    private ChangeTwoAdapter mChangeTwoAdapter;
    private String mProvinceName;
    private String mCityName;
    private String mAreaName;
    private List<String> mBeanAreaList;
    private List<ChoiceAddressListInfo.CityBean> mCityBeanList;


    @Override
    public int getLayout() {
        return R.layout.activity_change_address;
    }

    @Override
    protected IPresenter getPresenter() {
        return null;
    }


    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);
        fitterScreen();
        initData();
        initListview();
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        String addressJson = GetJsonDataUtil.getJson(this, "AddressJson");
        Gson gson = new Gson();
        Type listType = new TypeToken<ArrayList<ChoiceAddressListInfo>>() {}.getType();
        List<ChoiceAddressListInfo> mList = gson.fromJson(addressJson, listType);
        if (mList != null && mList.size() != 0) {


            ChangeOneMenuAdapter changeOneMenuAdapter = new ChangeOneMenuAdapter(mList, R.layout.item_first_menu);
            changeOneMenuAdapter.setSelectItem(0);
            rcv_one.setAdapter(changeOneMenuAdapter);

            //获取市的集合
            ChoiceAddressListInfo provinceList = mList.get(0);
            if (provinceList != null) {
                mProvinceName = provinceList.getName();
                mCityBeanList = provinceList.getCity();
                mChangeTwoAdapter = new ChangeTwoAdapter(mCityBeanList, R.layout.item_child_city);
                mChangeTwoAdapter.setSelectItem(0);
                rcv_two.setAdapter(mChangeTwoAdapter);


                //获取区的集合
                ChoiceAddressListInfo.CityBean cityBean = mCityBeanList.get(0);
                if (cityBean != null) {
                    mCityName = cityBean.getName();
                    mBeanAreaList = cityBean.getArea();
                    if (mBeanAreaList != null && mBeanAreaList.size() != 0) {
                        mChangeThreeAdapter = new ChangeThreeAdapter(mBeanAreaList, R.layout.item_child_city);
                        mChangeThreeAdapter.setSelectItem(0);
                        rcv_three.setAdapter(mChangeThreeAdapter);
                        mAreaName = mBeanAreaList.get(0);
                    }
                }


                changeOneMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (mList != null && mList.size() != 0) {
                            ChoiceAddressListInfo dataBean = mList.get(position);
                            if (dataBean != null) {
                                if (changeOneMenuAdapter != null) {
                                    changeOneMenuAdapter.setSelectItem(position);
                                    changeOneMenuAdapter.notifyDataSetChanged();
                                }
                                mProvinceName = dataBean.getName();
                                mCityBeanList = dataBean.getCity();
                                if (mCityBeanList != null && mCityBeanList.size() != 0) {
                                    mChangeTwoAdapter.replaceData(mCityBeanList);
                                    mChangeTwoAdapter.setSelectItem(0);
                                    mChangeTwoAdapter.notifyDataSetChanged();

                                    ChoiceAddressListInfo.CityBean cityBean = mCityBeanList.get(0);
                                    if (cityBean != null) {
                                        mCityName = cityBean.getName();
                                        List<String> areaList = cityBean.getArea();
                                        mChangeThreeAdapter.replaceData(areaList);
                                        mChangeThreeAdapter.setSelectItem(0);
                                        mChangeThreeAdapter.notifyDataSetChanged();
                                    }
                                }
                            }
                        }
                    }
                });


                mChangeTwoAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (mChangeTwoAdapter != null) {
                            mChangeTwoAdapter.setSelectItem(position);
                            mChangeTwoAdapter.notifyDataSetChanged();
                        }

                        ChoiceAddressListInfo.CityBean cityBean = mCityBeanList.get(position);
                        mCityName = cityBean.getName();
                        mBeanAreaList = cityBean.getArea();
                        if (mBeanAreaList != null && mBeanAreaList.size() != 0) {
                            mChangeThreeAdapter.replaceData(mBeanAreaList);
                            mChangeThreeAdapter.setSelectItem(0);
                            mChangeThreeAdapter.notifyDataSetChanged();

                        }
                    }
                });




                mChangeThreeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        String areaName = mBeanAreaList.get(position);
                        Intent intent = new Intent();
                        intent.putExtra("province", mProvinceName);
                        intent.putExtra("city", mCityName);
                        intent.putExtra("area", areaName);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });

            }

        }

    }

    /**
     * 初始化listview
     */
    private void initListview() {
        rcv_one.setLayoutManager(new LinearLayoutManager(this));
        rcv_two.setLayoutManager(new LinearLayoutManager(this));
        rcv_three.setLayoutManager(new LinearLayoutManager(this));
    }

    /**
     * 适配刘海
     */
    private void fitterScreen() {
        if (mIsNeed) {
            int pH = DisplayCutoutUtil.getStatusBarHeight(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.height = pH;
            fake_status_bar.setLayoutParams(params);
        }
    }

//    /**
//     * 获取所有省份信息
//     *
//     * @param info
//     */
//    @Override
//    public void onProvincesList(ProvinceAndCityInfo info) {
//        int status = info.getStatus();
//        switch (status) {
//            case 200:
//                mBeanList = info.getData();
//                ChangeOneMenuAdapter  mOneMenuAdapter = new ChangeOneMenuAdapter(mBeanList, R.layout.item_first_menu);
//                mOneMenuAdapter.setSelectItem(0);
//                rcv_one.setAdapter(mOneMenuAdapter);
//                mOneMenuAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        if (mBeanList != null && mBeanList.size() != 0) {
//                            ProvinceAndCityInfo.DataBean dataBean = mBeanList.get(position);
//                            if (dataBean != null) {
//                                mProvinceValue = dataBean.getValue();
//                                mProvinceLabel = dataBean.getLabel();
//                                mPresenter.getProvinceToCity(mProvinceLabel);
//                                if (mOneMenuAdapter != null) {
//                                    mOneMenuAdapter.setSelectItem(position);
//                                    mOneMenuAdapter.notifyDataSetChanged();
//                                }
//                            }
//                        }
//                    }
//                });
//
//                if (mBeanList != null && mBeanList.size() != 0) {
//                    ProvinceAndCityInfo.DataBean dataBean = mBeanList.get(0);
//                    mProvinceValue = dataBean.getValue();
//                    //获取二级目录数据
//                    mPresenter.getProvinceToCity(dataBean.getLabel());
//                }
//                break;
//            case 422:
//                String msg = info.getMsg();
//                showToast(msg);
//                break;
//            default:
//                break;
//        }
//    }
//
//    /**
//     * 根据省份获取城市
//     *
//     * @param info
//     */
//    @Override
//    public void onProvinceToCity(ProvinceAndCityInfo info) {
//        int status = info.getStatus();
//        switch (status) {
//            case 200:
//                List<ProvinceAndCityInfo.DataBean> dataList = info.getData();
//                ChangeProvinceAdapter provinceAdapter = new ChangeProvinceAdapter(dataList, R.layout.item_child_city);
//                rcv_two.setAdapter(provinceAdapter);
//
//                //默认选择第一个
//                if (dataList != null && dataList.size() != 0) {
//                    ProvinceAndCityInfo.DataBean dataBean = dataList.get(0);
//                    if (dataBean != null) {
//                        mCityLabel = dataBean.getLabel();
//                        mCityValue = dataBean.getValue();
//                        mPresenter.getCityToArea(mCityLabel);
//                        if (provinceAdapter != null) {
//                            provinceAdapter.setSelectItem(0);
//                            provinceAdapter.notifyDataSetChanged();
//                        }
//                    }
//                }
//
//                provinceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        if (dataList != null && dataList.size() != 0) {
//                            ProvinceAndCityInfo.DataBean dataBean = dataList.get(position);
//                            mCityValue = dataBean.getValue();
//                            mCityLabel = dataBean.getLabel();
//                            mPresenter.getCityToArea(mCityLabel);
//                            if (provinceAdapter != null) {
//                                provinceAdapter.setSelectItem(position);
//                                provinceAdapter.notifyDataSetChanged();
//                            }
//                        }
//                    }
//                });
//
//                break;
//            case 422:
//                String msg = info.getMsg();
//                showToast(msg);
//                break;
//            default:
//                break;
//        }
//    }
//
//    /**
//     * 根据城市获取区
//     *
//     * @param info
//     */
//    @Override
//    public void onCityToArea(ProvinceAndCityInfo info) {
//        switch (info.getStatus()) {
//            case 200:
//                List<ProvinceAndCityInfo.DataBean> infoData = info.getData();
//                ChangeProvinceAdapter provinceAdapter = new ChangeProvinceAdapter(infoData, R.layout.item_child_city);
//                rcv_three.setAdapter(provinceAdapter);
//                provinceAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                        if (infoData != null && infoData.size() != 0) {
//                            ProvinceAndCityInfo.DataBean dataBean = infoData.get(position);
//                            String label = dataBean.getLabel();
//                            String value = dataBean.getValue();
//                            Intent intent = new Intent();
//                            intent.putExtra("provinceValue", mProvinceValue);
//                            intent.putExtra("provinceLabel", mProvinceLabel);
//                            intent.putExtra("cityValue", mCityValue);
//                            intent.putExtra("cityLabel", mCityLabel);
//                            intent.putExtra("areaValue", value);
//                            intent.putExtra("areaLabel", label);
//                            setResult(RESULT_OK, intent);
//                            finish();
//                        }
//                    }
//                });
//                break;
//            case 422:
//                showToast(info.getMsg());
//                break;
//            default:
//                break;
//        }
//    }
//
//    @Override
//    public void onFailer(Throwable throwable) {
//
//    }
}
